package com.walker.taogong.bl.user;

import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.walker.taogong.dal.UserDal;
import com.walker.taogong.datamodel.User;


public class UserView implements UserManagement
{
    
    /** Logger. */
    private static Logger log = LoggerFactory.getLogger(UserView.class);
    
    
    private String userName;
    private String password;
    private String passwordTwo;
    private String mobile;
    private String email;
     
    public String getUserName()
    {
        return userName;
    }
    public void setUserName(String userName)
    {
        this.userName = userName;
    }
    public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    public String getPasswordTwo()
    {
        return passwordTwo;
    }
    public void setPasswordTwo(String passwordTwo)
    {
        this.passwordTwo = passwordTwo;
    }

    public String getMobile()
    {
        return mobile;
    }
    public void setMobile(String mobile)
    {
        this.mobile = mobile;
    }
    
    public String getEmail()
    {
        return email;
    }
    public void setEmail(String email)
    {
        this.email = email;
    }
    
    
    /************************ 服务引用 *********************************************/
    
    private UserDal userDal;
    
    public void setUserDao(UserDal userDal)
    {
        this.userDal = userDal;
    }
    
    /*************************interface 实现***************************************/
    
    @Override
    public boolean regist(UserView userView)
    { 
        User user = userDal.getUserByEmail(userView.getEmail());
        if(user == null)
            user = userDal.getUserByMobile(userView.getMobile());
        
        if(user == null)
        {
            user = new User();
            user.setUserName(userView.getUserName());
            user.setPassword(userView.getPassword());
            user.setEmail(userView.getEmail());
            user.setMobile(userView.getMobile());
            //1 表示新注册，未在线  2表示在线 3表示下线
            user.setStatus(1);
            this.userDal.saveUser(user);
            return true;
        }
        else
        {
            return false;
        }

    }
    @Override
    public boolean login(UserView userView)
    {
        User user = userDal.getUserByEmailAndPwd(userView.getEmail(), userView.getPassword());
        if(user==null)
        {
            user = userDal.getUserByMobileAndPwd(userView.getMobile(), userView.getPassword());
        }
        
        if(user == null)
            return false;

        return true;
    }
    
    
    @Override
    public boolean logout(UserView userView)
    {
        User user = userDal.getUserByUserName(userView.getUserName());
        user.setStatus(1);
        userDal.saveUser(user);
        return true;
        
    }
    
    @Override
    public boolean changePassword(UserView userView)
    {
        User user = userDal.getUserByUserName(userView.getUserName());
        if(user.getPassword().equals(userView.getPassword()))
        {
            user.setPassword(userView.getPasswordTwo());
            userDal.saveUser(user);
            return true;
        }
        return false;
    }
    
    
    /*************************启动测试用************************************/
    /**
     * 组件启动初期化
     */
    protected void activate(ComponentContext cc) throws Exception
    {
        log.info("starting main component");

        // 启动HibernateInitializer服务
        //FIXME 考虑移到别处，一次性装载
        cc.locateService("HibernateInitializer");
        
        //主流程
        
        //1. 注册用户
        this.userName = "zhang san";
        this.password = "123456";
        this.passwordTwo = "123456";
        
        //email或mobile任选其一
        this.setEmail("zhangsan@test.com") ;  //
        this.mobile = "12345678"    ;   //optional
        
        
        if( regist(this))
        {
            log.info("你已经成功注册完成:" + this.userName);
        }
        else
        {
            log.info("用户已经存在！");
        }
        
        if(login(this))
        {
            log.info(this.userName + "已经成功登陆");
        }
        else
        {
            log.info("输入的用户信息不正，请重新输入");
        }
        
    }

}
